import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:rainflutter/config/style.dart';

class NavigationBar extends StatelessWidget implements PreferredSizeWidget {
  /// 标题
  final String title;

  ///bar 两边按钮 如保存 等
  final List<Widget> actions;

  ///是否展示bar下边的黑线
  final bool showLine;

  ///是否展示返回按钮
  final bool showBack;

  ///返回按钮颜色
  final Color backBtnColor;

  /// bar背景颜色
  final backgroundColor;

  ///bar文字颜色
  final titleColor;

  final customHeight;

  ///light表示状态栏文字颜色是白色，否则黑色
  final bool isLight;

  final PreferredSizeWidget bottom;

  /// 事件 默认不需要
  /// 如果需要 则自己实现pop
  final Function() onPressed;
  NavigationBar(
    this.title, {
    Key key,
    this.actions,
    this.showLine = true,
    this.showBack = true,
    this.backBtnColor = Colors.black,
    this.backgroundColor = EsColors.White,
    this.titleColor = Colors.black,
    this.isLight = true,
    this.onPressed,
    this.customHeight = kToolbarHeight,
    this.bottom,
  }) : super(key: key);

  @override
  Size get preferredSize => new Size.fromHeight(customHeight);

  @override
  Widget build(BuildContext context) {
    return AppBar(
      iconTheme: IconThemeData(color: Colors.black),
      backgroundColor: backgroundColor,
      elevation: showLine ? 0.5 : 0.0,
      title: Text(title ?? '',
          style: TextStyle(
              fontSize: EsSizes.LG,
              color: titleColor,
              fontWeight: EsFontWeights.Medium)),
      centerTitle: true,
      brightness: isLight ? Brightness.light : Brightness.dark,
      actions: actions,
      //automaticallyImplyLeading: showBack, //系统的返回按钮
      leading: Visibility(
        visible: showBack,
        child: Semantics.fromProperties(
          properties: SemanticsProperties(label: 'navigator_back'),
          child: IconButton(
              icon: Icon(
                Platform.isAndroid ? Icons.arrow_back : Icons.arrow_back_ios,
                color: backBtnColor,
                size: EsSizes.MD,
              ),
              onPressed: onPressed == null ? () => _pop(context) : (onPressed)),
        ),
      ),
      bottom: bottom,
    );
  }

  void _pop(BuildContext context) {
    Navigator.of(context).pop();
  }
}
